6 红<template>
  <div class="app-container customer-div">
    <div style="margin-top: 20px">
      <!--搜索-->
      <el-form :inline="true" :model="listQuery" size="mini" label-width="130px">

        <el-form-item label="客户名称：">
          <el-select
            clearable
            v-model="listQuery.customerId"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="querySearch">
            <el-option
              v-for="item in querySearchList"
              :key="item.customerId"
              :label="item.customerName"
              :value="item.customerId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="业务员：">
          <el-cascader
            size="mini"
            collapse-tags
            :show-all-levels="false"
            v-model="listQuery.saleUserId"
            placeholder="请选择部门/事业部/人员"
            :options="userList"
            :props="props"></el-cascader>
        </el-form-item>
        <el-form-item label="客户分类：" >
          <el-select v-model="listQuery.customerNewType" clearable>
            <el-option
              v-for="item in customerTypeList"
              :key="item.cddValue"
              :label="item.cddValue"
              :value="item.cddValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户性质：">
          <el-select v-model="listQuery.customerQuality" clearable>
            <el-option v-for="item in customerQualityList"  :key="item.cddId"  :value="item.cddId" :label="item.cddValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规模性质：">
          <el-select v-model="listQuery.customerNature" clearable>
            <el-option v-for="(item,index) in newNatureList" :key="index" :value="item" :label="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跟进人：">
          <el-select v-model="listQuery.trackingUserId" filterable placeholder="请选择" clearable>
            <el-option
              v-for="item in userOptionList"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跟进方式：">
          <el-select  v-model="listQuery.trackingMethod" placeholder="请选择" clearable>
            <el-option v-for="item in customerTrackingList"  :value="item.cddValue" :label="item.cddValue" :key="item.cddId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跟进时间：">
          <el-date-picker
            v-model="listQuery.trackingTime"
            type="daterange"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="跟进内容：">
          <el-input clearable v-model="listQuery.trackingContent"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="handleSearchList()"
            type="primary">
            查询
          </el-button>
          <el-button
            :loading="btnloading"
            @click="downloadExcel()"
            type="primary">
            导出
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--数据列表-->
    <div class="table-container">
      <el-table
        v-loading="loading"
        :data="list"
        border
        style="width: 100%; margin-top: 20px">

        <el-table-column
          min-width="200"
          label="客户名称"
          align="center">
          <template slot-scope="scope">
            <div>{{scope.row.customerName}}</div>
          </template>
        </el-table-column>
        <el-table-column
          min-width="100"
          prop="saleUserName"
          label="销售员"
          align="center">
        </el-table-column>
        <el-table-column
          min-width="100"
          prop="customerNewType"
          align="center"
          label="客户分类">
        </el-table-column>
        <el-table-column
          min-width="100"
          prop="customerQuality"
          align="center"
          label="客户性质">
        </el-table-column>
        <el-table-column
          min-width="100"
          prop="customerNature"
          align="center"
          label="规模性质">
        </el-table-column>
        <el-table-column
          min-width="100"
          prop="trackingUserName"
          align="center"
          label="跟进人">
        </el-table-column>
        <el-table-column
          min-width="100"
          prop="trackingTime"
          align="center"
          label="跟进时间">
        </el-table-column>
        <el-table-column
          min-width="100"
          prop="createdTime"
          align="center"
          label="创建时间">
        </el-table-column>
        <el-table-column
          min-width="100"
          prop="trackingMethod"
          align="center"
          label="跟进方式">
        </el-table-column>
        <el-table-column
          min-width="200"
          prop="trackingContent"
          align="center"
          label="跟进内容">
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="count">
      </el-pagination>
    </div>

  </div>
</template>

<script>
import { getOrderUsersList,getCustomerOption,getConfigList,getCustomerSetting} from '@/api/partner'
import {getCustomerInfoAPI} from '@/api/company'
import {manageTrackingList} from '@/api/fms'
import {saleUserList} from '@/api/data.js'
import {formatDate} from '@/utils/date';
import store from '@/store'
import { mapGetters } from 'vuex'
const defaultListQuery={
  "customerId" : '', //客户ID
  "saleUserId" : [], //业务员
  "customerNewType" : "", //客户分类
  "customerQuality" : '', //客户性质
  "customerNature" : "", //规模性质
  "trackingUserId" : '', //跟进人
  "trackingTime" : [],//跟进时间
  "trackingContent" : "",//跟进内容
  "trackingMethod" : "", //跟进方式
  "pageNum" : 1,
  "pageSize" : 10,
  "isExport" : '' //是否导出
};
export default {
  name: "manageFmsList",
  data(){
    return{
      roles:store.getters.roles ? store.getters.roles : [],
      listQuery:JSON.parse(JSON.stringify(defaultListQuery)),
      list:[],
      count:0,
      loading:false,
      userList:[],
      customerList:[],
      loading2:false,
      btnloading:false,
      props:{multiple: true,label:'value',value:'label',emitPath:false},
      querySearchList:[],
      customerTypeList:[],
      customerQualityList:[],
      customerTrackingList:[],
      newNatureList:[],
      userOptionList:[]

    }
  },
  computed: {
    ...mapGetters([
      'name',
      'mobile',
      'userId'
    ]),


  },
  created() {
    this.getListData();
    this.getUserList();
    this.getCustomerType();
    this.getCustomerSettingList();
    this.getCustomerInfo();
    this.getUserOptionList()

  },
  filters: {
    formatTime(time) {
      let date = new Date(time);
      return formatDate(date, 'yyyy-MM-dd')
    },
  },
  methods:{
    getUserList(){
      saleUserList().then((res)=>{
        console.log(res)
        this.userList = res
      })
    },
    //获取订单分类列表
    getCustomerType(){
      getConfigList({status:1}).then((res)=>{
        let customerType = res.find((el)=>{
          return  el.cdtCode == 'customer_new_type'
        })
        this.customerTypeList = customerType ? customerType.data : []
      })
    },
    getCustomerSettingList(){
      getCustomerSetting().then((res)=>{
        res.forEach((el)=>{
         if (el.cdtCode == 'customer_quality') {//客户性质
            this.customerQualityList = el.data;
          }else if(el.cdtCode == 'manage_tracking_method'){
            this.customerTrackingList = el.data;
         }
        });
      })
    },
    // 获取客户信息配置
    getCustomerInfo() {
      getCustomerInfoAPI({tab: 2}).then((res) => {
        this.newNatureList = res;
      })
    },
    //下载excle
    downloadExcel(){
      let listQuery = JSON.parse(JSON.stringify(this.listQuery));
      listQuery.isExport = 1;
      this.btnloading = true;
      manageTrackingList(listQuery).then((res)=>{
        this.btnloading = false;
        require.ensure([], () => {
          const { export_json_to_excel } = require('sheet/Export2Excel');
          const tHeader = res.title;
          const filterVal = res.columns;
          const list = res.list;
          const data = this.formatJson(filterVal, list);
          export_json_to_excel(tHeader, data, res.fileName);
        })
      }).catch(()=>{
        this.btnloading = false;
      })
    },

    //下载表格数据格式化
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    //获取员工下拉选项
    getUserOptionList(){
      getOrderUsersList({status:1}).then((res)=>{
        this.userOptionList = res;
      })
    },

    //客户名称输入匹配
    querySearch(queryString){
      let data = {};
      data.keywords = queryString;
      getCustomerOption(data).then((res)=>{
        this.querySearchList = res;
      })
    },


    handleSearchList(){
      this.listQuery.pageNum = 1;
      this.getListData();
    },

    handleSizeChange(val){
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getListData();
    },
    handleCurrentChange(val){
      this.listQuery.pageNum = val;
      this.getListData();
    },

    getListData(){
      this.loading = true;
      manageTrackingList(this.listQuery).then((res)=>{
        this.list = res.list;
        this.count = res.count;
        this.loading = false;
      }).catch(()=>{
        this.loading = false;
      })
    },

  },
}
</script>


